﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:SYS_ORG
	/// </summary>
	public partial class SYS_ORG : BaseDAL<GSLMS.Entity.TB_SYS_ORG>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public SYS_ORG()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_SYS_ORG model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_ORGNO", model.ORGNO),
								new OracleParameter("P_INTNO", model.INTNO),
								new OracleParameter("P_ORGNAME", model.ORGNAME),
								new OracleParameter("P_TELCODE", model.TELCODE),
								new OracleParameter("P_TEL", model.TEL),
								new OracleParameter("P_TELSUB", model.TELSUB),
								new OracleParameter("P_FAXCODE", model.FAXCODE),
								new OracleParameter("P_FAX", model.FAX),
								new OracleParameter("P_ADDR", model.ADDR),
								new OracleParameter("P_POSTNO", model.POSTNO),
								new OracleParameter("P_PRINCIPALNAME", model.PRINCIPALNAME),
								new OracleParameter("P_PRINCIPALID", model.PRINCIPALID),
								new OracleParameter("P_MOBILE", model.MOBILE),
								new OracleParameter("P_PID", model.PID),
								new OracleParameter("P_ISSITE", model.ISSITE),
								new OracleParameter("P_REMARK", model.REMARK)};
  
			for (int i = 0; i < 17; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_org_add", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_OrgList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_SYS_ORG model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_ORGNO", model.ORGNO),
								new OracleParameter("P_INTNO", model.INTNO),
								new OracleParameter("P_ORGNAME", model.ORGNAME),
								new OracleParameter("P_TELCODE", model.TELCODE),
								new OracleParameter("P_TEL", model.TEL),
								new OracleParameter("P_TELSUB", model.TELSUB),
								new OracleParameter("P_FAXCODE", model.FAXCODE),
								new OracleParameter("P_FAX", model.FAX),
								new OracleParameter("P_ADDR", model.ADDR),
								new OracleParameter("P_POSTNO", model.POSTNO),
								new OracleParameter("P_PRINCIPALNAME", model.PRINCIPALNAME),
								new OracleParameter("P_PRINCIPALID", model.PRINCIPALID),
								new OracleParameter("P_MOBILE", model.MOBILE),
								new OracleParameter("P_PID", model.PID),
								new OracleParameter("P_ISSITE", model.ISSITE),
								new OracleParameter("P_REMARK", model.REMARK)};
  
			for (int i = 0; i < 17; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_org_update", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_OrgList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_sys.proc_org_delete", parms);
                //清除缓存
                OracleHelper.RemoveCacheObject("_OrgList");
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_SYS_ORG DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_SYS_ORG model = new GSLMS.Entity.TB_SYS_ORG();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["ORGNO"]!=null && dr["ORGNO"].ToString()!="")
				{
					model.ORGNO=dr["ORGNO"].ToString();
				}
				if(dr["INTNO"]!=null && dr["INTNO"].ToString()!="")
				{
					model.INTNO=dr["INTNO"].ToString();
				}
				if(dr["ORGNAME"]!=null && dr["ORGNAME"].ToString()!="")
				{
					model.ORGNAME=dr["ORGNAME"].ToString();
				}
				if(dr["TELCODE"]!=null && dr["TELCODE"].ToString()!="")
				{
					model.TELCODE=dr["TELCODE"].ToString();
				}
				if(dr["TEL"]!=null && dr["TEL"].ToString()!="")
				{
					model.TEL=dr["TEL"].ToString();
				}
				if(dr["TELSUB"]!=null && dr["TELSUB"].ToString()!="")
				{
					model.TELSUB=dr["TELSUB"].ToString();
				}
				if(dr["FAXCODE"]!=null && dr["FAXCODE"].ToString()!="")
				{
					model.FAXCODE=dr["FAXCODE"].ToString();
				}
				if(dr["FAX"]!=null && dr["FAX"].ToString()!="")
				{
					model.FAX=dr["FAX"].ToString();
				}
				if(dr["ADDR"]!=null && dr["ADDR"].ToString()!="")
				{
					model.ADDR=dr["ADDR"].ToString();
				}
				if(dr["POSTNO"]!=null && dr["POSTNO"].ToString()!="")
				{
					model.POSTNO=dr["POSTNO"].ToString();
				}
				if(dr["PRINCIPALNAME"]!=null && dr["PRINCIPALNAME"].ToString()!="")
				{
					model.PRINCIPALNAME=dr["PRINCIPALNAME"].ToString();
				}
				if(dr["PRINCIPALID"]!=null && dr["PRINCIPALID"].ToString()!="")
				{
					model.PRINCIPALID=OracleHelper.ToLong(dr["PRINCIPALID"].ToString());
				}
				if(dr["MOBILE"]!=null && dr["MOBILE"].ToString()!="")
				{
					model.MOBILE=dr["MOBILE"].ToString();
				}
				if(dr["PID"]!=null && dr["PID"].ToString()!="")
				{
					model.PID=OracleHelper.ToLong(dr["PID"].ToString());
				}
				if(dr["ISSITE"]!=null && dr["ISSITE"].ToString()!="")
				{
                    model.ISSITE = OracleHelper.ToInt(dr["ISSITE"].ToString());
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_SYS_ORG GetByID(long ID)
		{
			GSLMS.Entity.TB_SYS_ORG sModel = new GSLMS.Entity.TB_SYS_ORG();
			string sql = "";
			sql = "select * from TB_SYS_ORG where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

        /// <summary>
        /// 从缓存中获取组织机构信息
        /// </summary>
        /// <returns></returns>
        private DataTable GetAllByCache()
        {
            DataTable dt = null;
            if (OracleHelper.GetCachecObject("_OrgList") != null)
                dt = (DataTable)OracleHelper.GetCachecObject("_OrgList");
            if (dt == null || dt.Rows.Count == 0)
            {
                string sql = "";
                sql = "select * from tb_sys_org";
                DataSet ds = OracleHelper.ExecuteDataset(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    OracleHelper.AddCacheObject("_OrgList", (object)dt, 0);
                    //清除缓存
                    //OracleHelper.RemoveCacheObject("_OrgList");
                }

            }

            return dt;
        }

         /// <summary>
		/// 分页提取数据
		/// </summary>
        public List<GSLMS.Entity.TB_SYS_ORG> GetAllList(long notOrgID)
        {
            var list = new List<GSLMS.Entity.TB_SYS_ORG>();
            DataTable dt = null;

            dt = OracleHelper.DataTableFileter(GetAllByCache(), "  id<>"+notOrgID.ToString(), " INTNO asc");

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_SYS_ORG model = new GSLMS.Entity.TB_SYS_ORG();
                    model = DataRowToModel(dt.Rows[i]);
                    list.Add(model);
                }
            }

            return list;
        }

        /// <summary>
		/// 分页提取数据
		/// </summary>
        public List<GSLMS.Entity.TB_SYS_ORG> GetAllList()
        {
            var list = new List<GSLMS.Entity.TB_SYS_ORG>();
            DataTable dt = null;

            dt = OracleHelper.DataTableFileter(GetAllByCache(), "", " INTNO asc");

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_SYS_ORG model = new GSLMS.Entity.TB_SYS_ORG();
                    model = DataRowToModel(dt.Rows[i]);
                    list.Add(model);
                }
            }

            return list;
        }


		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_SYS_ORG> GetList(GSLMS.Entity.TB_SYS_ORG searchParam)
		{
			var list=new List<GSLMS.Entity.TB_SYS_ORG>();
			DataTable dt = null;
			string tblName = "TB_SYS_ORG";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_SYS_ORG model = new GSLMS.Entity.TB_SYS_ORG();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

